import {mapGetters} from 'vuex'
import {isNull} from '@/utils/type'
import {parseTime} from '@/utils'
import {parseStatus, parseCityCode} from '@/utils/code'
import {field} from './utils'

export default {
  data() {
    return {
      dict: null
    }
  },
  created() {
    this.dict = this.findParentDict(this)
  },
  computed: {
    ...mapGetters(['allCityData']),
    // 单元格对齐方式
    align() {
      if (this.$attrs.hasOwnProperty('align')) {
        return this.$attrs.align
      } else {
        return false
      }
    },
    // 排序
    sortable() {
      return this.$attrs.hasOwnProperty('sortable') ? 'custom' : false
    },
    hasSortableName() {
      if (this.sortable) {
        return this.$attrs.sortable == 'custom' ? false : this.$attrs.sortable
      } else {
        return false
      }
    },
    // 过滤
    filters() {
      if (this.$attrs.hasOwnProperty('filterable')) {
        return this.dict[this.$attrs.dictName].map(dict => ({...dict, text: dict.label}))
      } else {
        return undefined  // u-table-column 需要返回`undefined`取消过滤按钮的显示
      }
    }
  },
  methods: {
    isNull, parseTime, parseStatus, field,
    parseCityCode(code) {
      return parseCityCode(this.allCityData, code)
    },
    findParentDict(parent) {
      let dict
      find(parent)

      function find(parent) {
        if (!parent) return
        if (parent.dict) {
          dict = parent.dict
          return
        } else {
          find(parent.$parent)
        }
      }

      return dict
    },
    queryName(column) {
      if (this.$attrs.queryPro) {
        return this.$attrs.queryPro
      } else {
        return this.$attrs.searchable ? this.$attrs.searchable : column.property
      }
    },
    filterMethod(value, row, column) {
      return true
    }
  }
}